package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.annotation.NoNeedLogin;
import com.future.common.base.BaseController;
import com.future.common.exception.AppException;
import com.future.common.global.PlatFormContext;
import com.future.common.utils.IDUtil;
import com.future.model.SysUser;
import com.future.requestbean.UserAddBean;
import com.future.requestbean.UserModifyBean;
import com.future.service.SysUserService;
import com.jfinal.aop.Before;
/**
 * 用户信息controller
 * @author ThinkPad
 *
 */
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hutool.crypto.SecureUtil;
@ControllerMapping("/user")
public class SysUserController extends BaseController {
	@Inject
	private SysUserService sysUserService;
	@Inject
	private SysUser sysUser;
	
	public void getSysUserList(@NotNull(message = "请选择是否审核") String sfsh){
		renderResult(Kv.by("userlist", sysUserService.getUserList(sfsh)));
	}
	
	public void getUserDetail(@NotNull(message = "请选择用户") String userid){
		renderResult(Kv.by("userdetail",this.sysUser.findById(userid)));
	}
	
	@Before(Tx.class)
	public void userSh(@NotNull(message = "请选择用户") String userid){
		this.sysUser.findById(userid).setSfsh("1").update();
		renderSuccess();
	}
	public void getCurrentUserDetail(){
		renderResult(Kv.by("userdetail",PlatFormContext.getCurrentUser()));

	}
	
	@Before(Tx.class)
	public void passwordChange(@NotNull(message="用户信息为空！")String userid, @NotNull(message="旧密码为空！")String passwordold, @NotNull(message="新密码为空！")String passwordnew){
		this.sysUserService.passWordChange(userid, passwordold, passwordnew);
		renderSuccess();
	}
	
	@Before(Tx.class)
	public void appUserModify(){
		UserModifyBean userAddBean = getJsonBody(UserModifyBean.class);
		SysUser user = this.sysUser.findById(PlatFormContext.getCurrentUser().getUserid());
		
		if(user == null){
			throw new AppException("用户信息不存在！");
		}
		user.setSjh(userAddBean.getSjh());
		user.setXb(userAddBean.getXb());
		user.setCsrq(userAddBean.getCsrq());
		user.setSfzhm(userAddBean.getSfzhm());
		user.setJtzz(userAddBean.getJtzz());
		
		this.sysUserService.modifyUser(user);
		renderSuccess();
	}
	
	@Before(Tx.class)
	public void changeCurrentPassword(@NotNull(message="旧密码为空！")String passwordold, @NotNull(message="新密码为空！")String passwordnew){
		String userId = PlatFormContext.getCurrentUser().getUserid();
		this.sysUserService.passWordChange(userId, passwordold, passwordnew);
		renderSuccess();
	}
	@Before(Tx.class)
	@NoNeedLogin
	public void userAdd(){
		UserAddBean userAddBean = getJsonBody(UserAddBean.class);
		if(this.sysUser.find("select * from sys_user where username= ?",userAddBean.getUsername()).size()>0){
			throw new AppException("此用户名已经被占用了");
		}
		SysUser user = new SysUser();
		user.setUserid(IDUtil.getId());
		user.setUsername(userAddBean.getUsername());
		user.setPassword(SecureUtil.md5(userAddBean.getPassword()));
		user.setXm(userAddBean.getXm());
		user.setSjh(userAddBean.getSjh());
		user.setCsrq(userAddBean.getCsrq());
		user.setSfzhm(userAddBean.getSfzhm());
		user.setXb(userAddBean.getXb());
		user.setJtzz(userAddBean.getJtzz());
		user.setRole("1");
		user.save();
		renderSuccess();
	}
	
	public void getUserList(){
		renderResult(Kv.by("userlist",this.sysUser.find("select * from sys_user where sfsh ='1' ")));
	}
	
}
